package tool;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.sql.SQLException;
import java.util.List;

/**定义方法，创建各实体类
 * @author JiangFan
 */
public class CreateEntityClass {
    BaseDao baseDao=new BaseDao();

    /**添加所有entity类
     * @throws ClassNotFoundException
     * @throws SQLException
     * @throws IOException
     */
    public void entity()throws ClassNotFoundException, SQLException, IOException {
        try {
            //得到工程路径
            String projectPath=System.getProperty("xiaomi.dir");
            //得到实体类包路径
            String entityPath=projectPath+"\\src\\entity";
            //用来得到实体类包，把实体类创建到包下
            String newEntityPath=projectPath+"\\src\\entity\\";
            //创建实体类包
            baseDao.mkDirectory(entityPath);

            List tableName= baseDao.getTableNameList();
            //写入字符流的抽象类。子类必须实现的方法仅有
            // write(char[], int, int)、flush() 和 close()。
            // 但是，多数子类将重写此处定义的一些方法，以提供更高的效率和/或其他功能
            Writer writer=null;
            for (int i = 0; i < tableName.size(); i++) {
                //得到表名
                String entityName=""+tableName.get(i);
                //将首字母大写
                char[] initialCapital=entityName.toCharArray();
                initialCapital[0]-=32;
                String newEntityName=entityName.valueOf(initialCapital);
                //创建实体类文件
                File dest=new File(newEntityPath+newEntityName+"Entity.java");
                //创建输出流对象
                writer=new FileWriter(dest);
                //根据表名得到数据类型集合
                List columnType=baseDao.getColumnTypes(String.valueOf(tableName.get(i)));
                //根据表名得到字段名
                List columnName=baseDao.getColumnNameList(String.valueOf(tableName.get(i)));
                //存放包的位置
                writer.write("package entity;\r\n");
                //导入常用的包
                writer.append("import java.sql.Date;\r\n ");
                //创建实体类
                writer.append("public class "+newEntityName+"Entity{\r\n");
                for (int j = 0; j <columnName.size() ; j++) {
                    String type="";
                    if (columnType.get(j).equals("INT")){
                        type="Integer";
                    }else if (columnType.get(j).equals("TIMESTAMP") || columnType.get(j).equals("DATETIME")
                    ||columnType.get(j).equals("DATE")) {
                        type = "Date";

                    } else if (columnType.get(j).equals("DOUBLE")) {
                        type = "Double";

                    } else if (columnType.get(j).equals("DECIMAL")) {
                        type = "BigDecimal";
                    } else if (columnType.get(j).equals("CHAR") || columnType.get(j).equals("VARCHAR")) {
                        type = "String";
                    }
                    //创建变量
                    writer.append("\t"+"private "+type+" "+columnName.get(j)+";\r\n");
                }
                //根据得到的数据库名称类型创建get，set方法
                for (int j = 0; j < columnName.size(); j++) {
                    String type="";
                    if (columnType.get(j).equals("INT")){
                        type="Integer";
                    }else if (columnType.get(j).equals("TIMESTAMP") || columnType.get(j).equals("DATETIME")
                    ||columnType.get(j).equals("DATE")) {
                        type = "Date";

                    } else if (columnType.get(j).equals("DOUBLE")) {
                        type = "Double";

                    } else if (columnType.get(j).equals("DECIMAL")) {
                        type = "BigDecimal";
                    } else if (columnType.get(j).equals("CHAR") || columnType.get(j).equals("VARCHAR")) {
                        type = "String";
                    }
                    // 将变量首字母改为大写
                    String ent = "" + columnName.get(j);
                    char[] cs1 = ent.toCharArray();
                    cs1[0] -= 32;
                    String e = ent.valueOf(cs1);
                    // 创建get set方法
                    writer.append("\t"+"public " + type + " get" + e + "(){\r\n");
                    writer.append("\t\t"+"return " + ent + ";\r\n");
                    writer.append("}\r\n");
                    writer.append("\t"+"public void set" + e + "(" + type + " " + ent + "){\r\n");
                    writer.append("\t\t"+"this." + ent + " =" + ent + ";\r\n");
                    writer.append("}\r\n");
                }
                // 创建无参构造方法
                writer.append("\tpublic " + newEntityName + "Entity() {\r\n" + "	}\r\n");
                //writer.append("}\r\n");
                //创建含全部参数的构造方法
                writer.append("\tpublic "+newEntityName+"Entity(\r\n");
                int k=columnName.size();
                for (int j = 0; j <columnName.size() ; j++) {
                    String type="";
                    k--;
                    if (columnType.get(j).equals("INT")){
                        type="int";
                    }else if (columnType.get(j).equals("TIMESTAMP") || columnType.get(j).equals("DATETIME")
                            ||columnType.get(j).equals("DATE")) {
                        type = "Date";

                    } else if (columnType.get(j).equals("DOUBLE")) {
                        type = "double";

                    } else if (columnType.get(j).equals("CHAR") || columnType.get(j).equals("VARCHAR")) {
                        type = "String";
                    }
                    if (k>0){
                        writer.append("\t\t"+type+" "+columnName.get(j)+",\r\n");
                    }else {
                        writer.append("\t\t"+type+" "+columnName.get(j)+"){\r\n");
                    }
                }
                //writer.append("){\r\n");
                writer.append("\t\tsuper();\r\n");
                for (int j = 0; j <columnName.size() ; j++) {
                    writer.append("\t\tthis."+columnName.get(j)+"="+columnName.get(j)+";\r\n");
                }
                writer.append("\t}\r\n}\r\n");
                writer.flush();
            }
        }catch (Exception e){

        }
    }

    /**
     * 创建接口类，用来记录dao中使用到的方法
     * */
    public void implement() throws SQLException, ClassNotFoundException, IOException {
        //项目路径
        String projectPath=System.getProperty("xiaomi.dir");
        //类路径
        String mapperPath=projectPath+"\\dao\\mapper\\";
        baseDao.mkDirectory(mapperPath);
        List tableName=baseDao.getTableNameList();
        //写入字符流的抽象类
        Writer w=null;
        String c="";
        for (int i = 0; i <tableName.size() ; i++) {
            //获取表名
            String entityName=""+tableName.get(i);
            char[] cs=entityName.toCharArray();
            cs[0]-=32;
            //首字符大写
            String newEntityName=entityName.valueOf(cs);
            //新建类类名
            File dest=new File(mapperPath+newEntityName+"DAOI.java");
            //创建输出流对象
            w=new FileWriter(dest);
            List columnType=baseDao.getColumnTypes(String.valueOf(tableName.get(i)));
            List columnName=baseDao.getColumnNameList(String.valueOf(tableName.get(i)));
            w.write("package dao;\r\n");
            w.append("import java.sql.SQLException;\r\n");
            w.append("import entity."+newEntityName+"Entity;\r\n");
            w.append("/**\r\n*@author JiangFan\r\n*/\r\n");
            w.append("public interface "+newEntityName+"DAOI {\r\n\r\n}\r\n");
            w.flush();
        }
    }

    /**生成dao，同时使用结果集从数据库获取方法
     * @throws SQLException
     * @throws ClassNotFoundException
     * @throws IOException
     */
    public void mapper() throws SQLException, ClassNotFoundException, IOException {
        //项目路径
        String projectPath=System.getProperty("xiaomi.dir");
        //类路径
        String mapperPath=projectPath+"\\dao\\";
        baseDao.mkDirectory(mapperPath);
        List tableName=baseDao.getTableNameList();
        //写入字符流的抽象类
        Writer w=null;
        String c="";
        for (int i = 0; i <tableName.size() ; i++) {
            //获取表名
            String entityName=""+tableName.get(i);
            char[] cs=entityName.toCharArray();
            cs[0]-=32;
            //首字符大写
            String newEntityName=entityName.valueOf(cs);
            //新建类类名
            File dest=new File(mapperPath+newEntityName+"DAO.java");
            //创建输出流对象
            w=new FileWriter(dest);
            List columnType=baseDao.getColumnTypes(String.valueOf(tableName.get(i)));
            List columnName=baseDao.getColumnNameList(String.valueOf(tableName.get(i)));
            w.write("package dao.mapper;\r\n");
            w.append("import tool.RowMapper;\r\n");
            w.append("import tool.BaseDao;\r\n");
            w.append("import java.util.List;\r\n");
            w.append("import java.sql.SQLException;\r\n");
            w.append("import java.sql.ResultSet;\r\n");
            w.append("import entity."+newEntityName+"Entity;\r\n");
            w.append("import dao.mapper."+newEntityName+"DAOI;\r\n\r\n");
            w.append("/**\r\n*@author JiangFan\r\n*/\r\n");
            w.append("public class "+newEntityName+"DAO extends BaseDao implements " +
                     newEntityName+"DAOI {\r\n\r\n}\r\n");
            w.append("class "+newEntityName+"RowMapper implements RowMapper<"
                    +newEntityName+"Entity>{\r\n");
            w.append("\t@Override\r\n"+"\tpublic "+newEntityName
                    +"Entity rowmap(ResultSet set) throws  SQLException{\r\n");
            w.append("\t\treturn new "+newEntityName+"Entity(\r\n");
            int k=columnName.size();//计数，用来查询使用了多少次
            for (int j = 0; j < columnName.size(); j++) {
                k--;
                String type="";
                if (columnType.get(j).equals("INT")){
                    type="Int";
                }else if(columnType.get(j).equals("DOUBLE")){
                    type="Double";

                }else if(columnType.get(j).equals("DATETIME")||columnType.get(j).equals("DATE")){
                    type="Date";

                }else if(columnType.get(j).equals("CHAR")||columnType.get(j).equals("VARCHAR")){
                    type="String";

                }
                if (k>0){
                    w.append("\t\t\tset.get"+type+"(\""+columnName.get(j)+"\"),\r\n");
                }else {
                    w.append("\t\t\tset.get"+type+"(\""+columnName.get(j)+"\")\r\n");
                }

            }
           w.append("\t\t);\r\n");
          w.append("\t}\r\n");
            w.append("}\r\n");
            w.flush();
        }
    }

    public void intf() throws IOException{
        // 创建接口类
        String engineeringPath = System.getProperty("xiaomi.dir");
        String interfacePath = engineeringPath + "\\tool";
        baseDao.mkDirectory(interfacePath);
        File dest = new File(interfacePath + "\\RowMapper.java");
        Writer wr = null;
        wr = new FileWriter(dest);
        wr.write("package tool;\r\n" + "import java.sql.ResultSet;\r\n"
        +"import java.sql.SQLException;\r\n");
        wr.append("public interface  RowMapper<T> {\r\n" + "	/**\r\n" + "	 * @author  JiangFan\r\n"
                  + "	 * @return  返回一个具体的实体对象\r\n"
                + "	 */\r\n" + "	public T rowmap(ResultSet set) throws SQLException;\r\n" + "}\r\n" + "");
        wr.flush();
    }
}
